package cn.iocoder.yudao.module.member.service.jobcollection;

import cn.iocoder.yudao.framework.common.exception.ErrorCode;
import cn.iocoder.yudao.module.member.controller.admin.job.vo.AppJobRespVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.member.controller.admin.jobcollection.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.jobcollection.JobCollectionDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.member.dal.mysql.jobcollection.JobCollectionMapper;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

/**
 * 我的收藏 Service 实现类
 *
 * @author Alan
 */
@Service
@Validated
public class JobCollectionServiceImpl implements JobCollectionService {

    @Resource
    private JobCollectionMapper jobCollectionMapper;

    @Override
    public Long createJobCollection(JobCollectionSaveReqVO createReqVO) {
        // 插入
        JobCollectionDO jobCollection = BeanUtils.toBean(createReqVO, JobCollectionDO.class);
        jobCollectionMapper.insert(jobCollection);
        // 返回
        return jobCollection.getId();
    }

    @Override
    public Boolean getJobCollectionUser(Long loginUserId, Long id) {
        JobCollectionDO jobCollectionDO = jobCollectionMapper.selectOne(new LambdaQueryWrapper<JobCollectionDO>()
                .eq(JobCollectionDO::getUserId, loginUserId)
                .eq(JobCollectionDO::getJobId, id)
        );
        if (Objects.isNull(jobCollectionDO)){
            return false;
        }
        return true;
    }

    @Override
    public void updateJobCollection(JobCollectionSaveReqVO updateReqVO) {
        // 校验存在
        validateJobCollectionExists(updateReqVO.getId());
        // 更新
        JobCollectionDO updateObj = BeanUtils.toBean(updateReqVO, JobCollectionDO.class);
        jobCollectionMapper.updateById(updateObj);
    }

    @Override
    public void deleteJobCollection(Long id) {
        // 校验存在
        validateJobCollectionExists(id);
        // 删除
        jobCollectionMapper.deleteById(id);
    }

    @Override
    public void deleteJob(Long jobId) {
        JobCollectionDO jobCollectionDO = jobCollectionMapper.selectOne(new LambdaQueryWrapper<JobCollectionDO>()
                .eq(JobCollectionDO::getUserId, getLoginUserId())
                .eq(JobCollectionDO::getJobId, jobId)
        );
        jobCollectionMapper.deleteById(jobCollectionDO.getId());
    }

    private void validateJobCollectionExists(Long id) {
        if (jobCollectionMapper.selectById(id) == null) {
            throw exception(JOB_COLLECTION_NOT_EXISTS);
        }
    }

    @Override
    public JobCollectionDO getJobCollection(Long id) {
        return jobCollectionMapper.selectById(id);
    }

    @Override
    public PageResult<JobCollectionDO> getJobCollectionPage(JobCollectionPageReqVO pageReqVO) {
        return jobCollectionMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<AppJobRespVO> getAppJobCollectionPage(JobCollectionPageReqVO pageReqVO) {
        Page<AppJobRespVO> page = new Page<AppJobRespVO>(pageReqVO.getPageNo(), pageReqVO.getPageSize());
        IPage<AppJobRespVO> user = jobCollectionMapper.getAppJobCollectionPage(page,pageReqVO);
        return new PageResult<>(user.getRecords(),user.getTotal());
    }
}